package com.itboye.ihomebank.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.WodeFangyYuanAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import com.scwang.smartrefresh.layout.api.RefreshLayout;

import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import static com.itboye.ihomebank.activity.home.ActivityRenZheng.ActivityRenZheng_ResultOk;
import static com.itboye.ihomebank.adapter.WodeFangyYuanAdapter.WodeFangyYuanAdapter_REQUEST;

/**
 * 我的发布
 * 
 */
public class WodeFangyuanActivity extends BaseOtherActivity implements Observer {
	View v_statusbar;
	SmartRefreshLayout refresh;
	private ListView xlistView;
	private HousePresenter housePresenter;
	private String uid;
	private WodeFangyYuanAdapter adapter;
	private List<WoDeFangYuanBean> arrayHouse;
	private ImageView close_icon;
	private TextView add_shap_title_tv;
	TextView imgEmpty;
	String role;
	private int pageNo = 1;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_wodefangyuan;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);

		showProgressDialog("查找中,请稍后",false);
		housePresenter = new HousePresenter(this);
		if ((SPUtils.get(this, null, SPContants.IdentityValidate, "") + "")
				.equals("1"))// 是房东
		{
			role = "fangdong";
		}
		if ((SPUtils.get(this, null, SPContants.BrokerValidate, "") + "")
				.equals("1")) {// 是经纪人
			role = "jingjiren";
		}

		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		add_shap_title_tv.setText("我的发布");
		arrayHouse = new ArrayList<WoDeFangYuanBean>();
		caozuo();
		adapter = new WodeFangyYuanAdapter(this, arrayHouse,
				R.layout.item_wodeqiuzu);
		adapter.setRole(role);
		xlistView.setAdapter(adapter);

	}

	private void caozuo(){
		refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
		refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

		//触发自动刷新
		refresh.autoRefresh();
		refresh.setEnableAutoLoadmore(false);//禁止加载更多
		refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						housePresenter.getWoDeFaBu(uid);
					}
				}, 0);
			}
		});
		refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
		refresh.finishLoadmore();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {

		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.woDeFaBuList_success) {
				List<WoDeFangYuanBean> woDeFangYuanBeans = (List<WoDeFangYuanBean>) entity
						.getData();
				if (pageNo == 1) {//下拉刷新
					arrayHouse.clear();
					adapter.notifyDataSetChanged();
					//结束加载
					refresh.finishRefresh();
					refresh.finishLoadmore();
				}
				if (woDeFangYuanBeans != null) {
					if (woDeFangYuanBeans.size() <= 0) {
						imgEmpty.setVisibility(View.VISIBLE);
					} else {
						imgEmpty.setVisibility(View.GONE);
						arrayHouse.addAll(woDeFangYuanBeans);
						adapter.notifyDataSetChanged();
					}
				} else {
					imgEmpty.setVisibility(View.VISIBLE);
				}

			} else if (entity.getEventType() == HousePresenter.woDeFaBuList_fail) {
				ByAlert.alert(entity.getData());
				refresh.finishLoadmore();
				refresh.finishRefresh();
			}
		}
		try{
			closeProgressDialog();
		}catch (Exception e){

		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		housePresenter.getWoDeFaBu(uid);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == WodeFangyYuanAdapter_REQUEST&&resultCode== ActivityRenZheng_ResultOk){
			housePresenter.getWoDeFaBu(uid);
		}
	}
}
